/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package poker_sim;

public class PokerLogic {
    int[][] CardType = new int [9][4];
    
    int stagecount = 1;

    //InterfaceClass
    InterfaceClass UIC;
    //Players hand
    Card[][] Hand = new Card[9][2];
    //Tables cards
//    //Card[] TableCard = new Card[5];
    //All cards per player (hand + table)
    Card[][] PlayerOptions = new Card[9][7];
    //Player array
    Player[] player;
    //Table reference
    Table table;

    public PokerLogic(Table _table, InterfaceClass _UIC) {
        //Initialize Cards.
        for(int player = 0 ; player < 9; player++)
        {
            for(int cardnumber = 0; cardnumber < 2; cardnumber++)
            {
                Hand[player][cardnumber] = new Card();
            }
        }
        //Load the table var
        table = _table;
        //Load the players array
        player = table.thePlayers;
        UIC = _UIC;

    }
    public void AddCardType(int player, int cardnumber)
    {
        //Add 1 to the correct column(by selecting the card.type.position) for given player
        if(cardnumber < 2)
        {
            CardType[player][Hand[player][cardnumber].Type.position]++;
        }
        else if(cardnumber > 1 && cardnumber < 7)
        {
            cardnumber -= 2;
//            //CardType[player][TableCard[cardnumber].Type.position]++;
        }
            
    }
    
    
//    public String HandInfo(int player)
//    {
//        String Info = FirstCalculation(player);
//        return Info;
//    }
    
    public void FirstCalculation(int _player)
    {
        String UpdateText = "";
        //Reset stagecount
        stagecount = 1;
        //Reset the Carttype var
        for(int _playerID = 0; _playerID < 9; _playerID++)
        {
            for(int cardtype = 0; cardtype < 4; cardtype++)
            {
                CardType[_playerID][cardtype] = 0;
            }
        }
        //Load the hand of selected player
        Hand[_player] = table.thePlayers[_player].GetHand();
        //Add the type of the card to the cardtype var
        AddCardType(_player,0);
        AddCardType(_player,1);
        //Update info to the UI
        UIC.UpdateTypeTable(_player, stagecount);
        //Test if 1st card is bigger than 2nd
        if(Hand[_player][0].UniqueID % 13 > Hand[_player][1].UniqueID % 13)
        {
            //if so, give high card handrank
            player[_player].HandRank = HandRanking.HIGH_CARD;
            //return info to UI.
            //TODO load ui component with data
            if(Hand[_player][1].UniqueID % 13 == 0)
                UpdateText = player[_player].HandRank.RankName + " " + Hand[_player][1].CardName;
            else
                UpdateText = player[_player].HandRank.RankName + " " + Hand[_player][0].CardName;
        }
        //Test if 2nd card is bigger than 1st
        else if(Hand[_player][0].UniqueID % 13 < Hand[_player][1].UniqueID % 13)
        {
            //if so, give high card handrank
            player[_player].HandRank = HandRanking.HIGH_CARD;
            //return info to UI.
            //TODO load ui component with data
            if(Hand[_player][0].UniqueID % 13 == 0)
                UpdateText = player[_player].HandRank.RankName + " " + Hand[_player][0].CardName;
            else
                UpdateText = player[_player].HandRank.RankName + " " + Hand[_player][1].CardName;

        }
        //Test if cards have the same value
        else if(Hand[_player][0].UniqueID % 13 == Hand[_player][1].UniqueID % 13)
        {
            //if so, give one pair handrank
            table.thePlayers[_player].HandRank = HandRanking.ONE_PAIR;
            //return info to UI.
            //TODO load ui component with data
            UpdateText =  table.thePlayers[_player].HandRank.RankName + " of " + Hand[_player][1].CardName + "'s";
        }
        if(_player == 0)
            UIC.UpdateHandRank(UpdateText);
    }
    
    public String SecondCalculation(int player)
    {
        //under construction
        boolean upgraded = false;
        Card[] phand = table.thePlayers[player].GetHand();
        if(table.thePlayers[player].HandRank == HandRanking.ONE_PAIR)
        {
            for(int handcard = 0; handcard < 2;handcard++)
            {
                for(int tablecard = 0; tablecard < 3;tablecard++)
                {
//                    if(phand[handcard].Type.position == table.TableCard[tablecard].Type.position)
//                    {
//                        
//                    }
                }
            }
        }
        return "";
    }

//    public void onReceiveHand(int player) {
//        
//        
//        FirstCalculation(player);
//    }
    
    public void onHand()
    {
        
    }

    public void onFlop() {
//        TableCard = table.TableCard;
    }

    public void onTurn() {
//       TableCard = table.TableCard;
    }

    public void onRiver() {
//        TableCard = table.TableCard;
    }

    public String HandValue(int player) {
        int value;
        int c1;
        int c2;

        Hand[player] = table.thePlayers[player].GetHand();

        c1 = (Hand[player][0].UniqueID % 13) + 2;
        c2 = (Hand[player][1].UniqueID % 13) + 2;

        switch (stagecount) {
            case 1:
                if (c1 == c2) {
                    table.thePlayers[player].HandRank = HandRanking.ONE_PAIR;
                    return "Good Hand";
                } else {
                    if (c1 >= 10 && c2 >= 10) {
                        return "Good Hand";
                    }

                    return "Bad Hand";
                }
            default:
                return "";

        }

    }

    public void Nextstage() {
        stagecount++;
    }

}
